varying vec3 vv;
const float radius = 1.0;

void main()
{
    vec3 eye = gl_ModelViewMatrixInverse[3].xyz;
    float a = dot(vv,vv);
    float b = dot(vv,eye);
    float c = dot(eye,eye) - radius*radius;
    float delta = b*b - a*c;

    if(delta < 0.0)
        discard;

    delta = sqrt(delta);
    float t = (-b - delta)/a;
    vec4 p = vec4(eye + t*vv, 1.0);
    vec3 n = normalize(gl_NormalMatrix* p.xyz);

    p = gl_ModelViewMatrix * p;

    vec3 l = normalize(vec3(gl_LightSource[0].position - p));
    float diff = max(0.0, dot(l,n));

    gl_FragColor = gl_Color * (diff + 0.2);
    gl_FragColor.a = 1.0;
    p = gl_ProjectionMatrix * p;
    gl_FragDepth = ((gl_DepthRange.far - gl_DepthRange.near) * p.z/p.w + gl_DepthRange.near + gl_DepthRange.far)/2.0;
}
